﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Cpt_HotApple_v2.Actors
{
    public class Player
    {
        //Declare the player texture
        Texture2D texture;
        public Texture2D[] playerImages = new Texture2D[5];
        //Declare the player positions
        public Vector2 position = Vector2.Zero;
        public Vector2 drawPosition = Vector2.Zero;
        public int oldKeyState; // 1-up, 2-down, 3-left, 4-right

        //Declare a collision rectangle
        public Rectangle collisionRect;

        public void LoadPlayerImages(ContentManager Content)
        {
            playerImages[0] = Content.Load<Texture2D>(@"Sprites/playerUp");
            playerImages[1] = Content.Load<Texture2D>(@"Sprites/playerDown");
            playerImages[2] = Content.Load<Texture2D>(@"Sprites/playerLeft");
            playerImages[3] = Content.Load<Texture2D>(@"Sprites/playerRight");
            
        }

        public Player(ContentManager Content, Vector2 clientBounds)
        {
            //Load the player texture
            LoadPlayerImages(Content);
            texture = playerImages[1];

            //Set the players draw position
            drawPosition.X = (clientBounds.X / 2) - (texture.Width / 2);
            drawPosition.Y = (clientBounds.Y / 2) - (texture.Height / 2);
        }

        public void Update()
        {
            //Get input from the user
            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.Up))
            {
                position.Y -= 2;
                texture = playerImages[0];
                oldKeyState = 1;
            }
            if (newState.IsKeyDown(Keys.Down))
            {
                position.Y += 2;
                texture = playerImages[1];
                oldKeyState = 2;
            }
            if (newState.IsKeyDown(Keys.Left))
            {
                position.X -= 2;
                texture = playerImages[2];
                oldKeyState = 3;
            }
            if (newState.IsKeyDown(Keys.Right))
            {
                position.X += 2;
                texture = playerImages[3];
                oldKeyState = 4;
            }

            //Update the collision rect
            collisionRect = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        public void Draw()
        {
            //Draw the player
            Game1.spriteBatch.Draw(texture, drawPosition, Color.White);
        }
    }
}
